package com.ledim.widget.ptr.header;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.letv.android.young.client.R;
import com.ledim.utils.LogUtil;
import com.ledim.widget.ptr.PtrFrameLayout;
import com.ledim.widget.ptr.PtrUIHandler;
import com.ledim.widget.ptr.indicator.PtrIndicator;

/**
 * Created by ccx on 16/6/21.
 * 乐次元Listiew刷新头部view 新的pullrefresh listview 非XlistView
 */
public class LedimListViewHeader extends FrameLayout implements PtrUIHandler {

    private FrameLayout mContainer;
    private int mState = STATE_NORMAL;
    private ImageView iv_header_animator;
    private View ll_normal;

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;

    private AnimationDrawable mAnimationDrawable = null;

    public LedimListViewHeader(Context context) {
        super(context);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public LedimListViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LedimListViewHeader(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        FrameLayout.LayoutParams lp =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        mContainer = (FrameLayout) LayoutInflater.from(context)
                                                 .inflate(R.layout.pullrefresh_listview_header, null);
        addView(mContainer, lp);
        ll_normal = findViewById(R.id.ll_normal);
        iv_header_animator = (ImageView) findViewById(R.id.iv_header_animator);
        //headHeight = Tools.getHeight(mNormal);
        iv_header_animator.setImageResource(R.anim.pull_header_animation);
        mAnimationDrawable = (AnimationDrawable) iv_header_animator.getDrawable();
        //mAnimationDrawable.setOneShot(false);
        //mAnimationDrawable.start();

    }

    public void setState(int state) {
        if (state == mState) {
            return;
        }

        if (state == STATE_REFRESHING) {
            // 刷新时显示动画图片
            ll_normal.setVisibility(View.GONE);
            iv_header_animator.setVisibility(View.VISIBLE);
            mAnimationDrawable.start();
        } else {
            mAnimationDrawable.stop();
            ll_normal.setVisibility(View.VISIBLE);
            iv_header_animator.setVisibility(View.GONE);
        }

        mState = state;
    }

    //public void setVisiableHeight(int height) {
    //    if (height < 0) {
    //        height = 0;
    //    }
    //    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mReday.getLayoutParams();
    //    lp.height = height;
    //    mReday.setLayoutParams(lp);
    //}

    @Override
    public void onUIReset(PtrFrameLayout frame) {
        LogUtil.e("ledimheader onUIReset");
        setState(STATE_NORMAL);
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        //setState(STATE_READY);
        LogUtil.e("ledimheader onUIRefreshPrepare");
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        //setState(STATE_REFRESHING);
        setState(STATE_REFRESHING);
        LogUtil.e("ledimheader onUIRefreshBegin");
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        // setState(STATE_REFRESHING);
        LogUtil.e("ledimheader onUIRefreshComplete");
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status,
                                   PtrIndicator ptrIndicator) {
    }
}
